package cz.idsv.web.mvc.controller;

/**
 * Product Controller.
 * 
 * @author Jan Borzensky
 */
import java.util.Collection;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;

import cz.idsv.web.mvc.model.entity.ProductEntity;
import cz.idsv.web.mvc.model.service.ProductService;



/**
 * Product controller.
 * 
 * @author Jan Borzensky
 */
@Controller
@SessionAttributes(value = {"productEntity"})
public class ProductController {
	
	private static final String LIST_MODEL_KEY = "list";
	private static final String PRODUCT_ENTITY = "productEntity";
	
	protected static Logger logger = Logger.getLogger("ProductController"); 
	
	@Autowired()
	private ProductService productService;
	
	/**
     * <p>Searches for all product and returns them in a 
     * <code>Collection</code>.</p>
     * 
     * <p>Expected HTTP GET and request '/product/list'.</p>
     */
    @RequestMapping(value="/product/list", method=RequestMethod.GET)
    public @ModelAttribute(LIST_MODEL_KEY) Collection<ProductEntity> search() {
    	logger.debug("Received request to show all product"); 
    	
        return productService.findProduct();
    }
    
	/**
     * <p>Create new or update old product 
     * <code>ProductEntity</code>.</p>
     * 
     * <p>Expected HTTP GET and request '/product/form'.</p>
     */
    @RequestMapping(value="/product/form", method=RequestMethod.GET)
    public @ModelAttribute(PRODUCT_ENTITY)  ProductEntity getDocumentForm(@RequestParam(value = "id", required = false) Integer id) {
        if(id==null)return new ProductEntity();
        return productService.findProductById(id);
    }
    
	/**
     * <p>Save new or update old product 
     * <code>ProductEntity</code>.</p>
     * 
     * <p>Expected HTTP POST and response '/product/saveForm'.</p>
     */
    @RequestMapping(value="/product/saveForm", method=RequestMethod.POST)
    public String saveProductForm(ProductEntity productEntity, Model model) {
    	if(productEntity != null) 
    		productService.save(productEntity);
    	return "redirect:/product/list.html";
    }
    
    /**
     * <p>Deletes a product.</p>
     * 
     * <p>Expected HTTP POST and request '/product/delete'.</p>
     */
    @RequestMapping(value="/product/delete", method=RequestMethod.POST)
    public String delete(ProductEntity productEntity) {
    	logger.debug("Received request to delete product with id:"+ productEntity.getId());
        productService.delete(productEntity);
        return "redirect:/product/list.html";
    }
    
}
